<template>
  <div class="app-container">
    <div class="filter-container">
      <el-select
        v-model="listQuery.recruitmentId"
        class="filter-item"
        style="width: 200px;margin-right: 10px"
        placeholder="请选择职位"
      >
        <el-option
          v-for="item in recruitmentOptions"
          :key="item.recruitmentId"
          :label="item.recruitmentName"
          :value="item.recruitmentId"
        />
      </el-select>
      <el-date-picker
        v-model="interviewTime"
        type="daterange"
        align="right"
        unlink-panels
        range-separator="至"
        start-placeholder="邀约日期下限"
        end-placeholder="邀约日期上限"
        :picker-options="pickerOptions"
        value-format="yyyy-MM-dd HH:mm:ss"
        style="margin-right: 10px"
      />
      <el-button
        class="filter-item"
        style="margin-left: 10px;"
        size="medium"
        type="primary"
        icon="el-icon-search"
        @click="handleFilter"
      >
        搜索
      </el-button>
      <el-button
        class="filter-item"
        style="margin-left: 10px;"
        size="medium"
        type="primary"
        icon="el-icon-refresh"
        @click="reset"
      >
        重置
      </el-button>
    </div>

    <!--    数据展示-->
    <div class="table-item">
      <el-table
        :key="tableKey"
        :data="list"
        border
        fit
        highlight-current-row
        style="width: 100%;"
      >
        <el-table-column label="序号" width="50" align="center">
          <template slot-scope="scope">
            {{ listQuery.limit * (listQuery.page - 1) + (scope.$index + 1) }}
          </template>
        </el-table-column>
        <el-table-column label="姓名" align="center">
          <template slot-scope="{row}">
            <span>{{ row.name }}</span>
          </template>
        </el-table-column>
        <el-table-column label="联系电话" align="center">
          <template slot-scope="{row}">
            <span>{{ row.phone }}</span>
          </template>
        </el-table-column>
        <el-table-column label="联系邮箱" align="center">
          <template slot-scope="{row}">
            <span>{{ row.email }}</span>
          </template>
        </el-table-column>
        <el-table-column label="面试职位" align="center">
          <template slot-scope="{row}">
            <span>{{ row.recruitmentName }}</span>
          </template>
        </el-table-column>
        <el-table-column label="面试时间" align="center">
          <template slot-scope="{row}">
            <span>{{ $moment(row.interviewTime).format('YYYY-MM-DD HH:MM:SS') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="面试地点" align="center" :show-overflow-tooltip="true">
          <template slot-scope="{row}">
            <span>{{ row.interviewAddress }}</span>
          </template>
        </el-table-column>
        <el-table-column label="状态" align="center">
          <template slot-scope="{row}">
            <span>{{ row.status | dataFilter(interviewStatusOperations) }}</span>
          </template>
        </el-table-column>
        <el-table-column label="备注" align="center" :show-overflow-tooltip="true">
          <template slot-scope="{row}">
            <span>{{ row.remark }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" width="300%" class-name="small-padding fixed-width">
          <template slot-scope="{row}">
            <el-popconfirm
              v-if="row.status === 0"
              title="请确认是否再次发送面试邀约申请？"
              @onConfirm="InviteAgain(row.id)"
            >
              <el-button slot="reference" class="ml10" type="primary" plain size="mini">再次邀约</el-button>
            </el-popconfirm>
            <el-popconfirm
              v-if="row.status === 0"
              title="请确认是否撤回面试邀约申请？"
              @onConfirm="withdraw(row.id)"
            >
              <el-button slot="reference" class="ml10" type="warning" plain size="mini">撤回邀约</el-button>
            </el-popconfirm>
            <el-button size="mini" type="success" class="ml10" plain @click="handleEdit(row.id)">
              修改邀约
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="listQuery.page"
        :limit.sync="listQuery.limit"
        @pagination="getList"
      />
    </div>

    <!--    邀约面试-->
    <el-dialog title="邀约面试" :visible.sync="dialogFormVisible" width="30%">
      <el-form ref="ruleForm" :model="interviewForm" :rules="rules">
        <el-form-item label="面试地点" prop="interviewAddress">
          <el-input v-model="interviewForm.interviewAddress" style="width: 80%;" placeholder="输入面试地点" />
        </el-form-item>
        <el-form-item label="面试时间" prop="interviewTime">
          <el-date-picker
            v-model="interviewForm.interviewTime"
            type="datetime"
            style="width: 80%;"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="选择日期时间"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancelInterview">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import {
  getRecruitmentOptions
} from '@/api/recruitment'
import {
  fetchInterviewList,
  inviteAgain,
  withdrawInterview,
  getOne,
  updateInterview
} from '@/api/interview'
import { interviewStatusOperations } from '../../../utils/constants'
import Pagination from '@/components/Pagination/index'
export default {
  name: 'GenericInterview',
  components: { Pagination },
  props: {
    query: {
      type: Object,
      default: () => {
        return {
          status: null
        }
      }
    }
  },
  data() {
    return {
      tableKey: 0,
      list: null,
      total: 0,
      listQuery: {
        page: 1,
        limit: 10,
        status: this.query.status,
        recruitmentId: undefined,
        interviewTimeFrom: undefined,
        interviewTimeTo: undefined
      },
      interviewStatusOperations: interviewStatusOperations,
      recruitmentOptions: undefined,
      interviewTime: undefined,
      interviewForm: {
        id: undefined,
        deliveryId: undefined,
        interviewAddress: undefined,
        interviewTime: null
      },
      rules: {
        interviewAddress: [
          { required: true, message: '请输入面试地点', trigger: 'blur' }
        ],
        interviewTime: [
          { required: true, message: '请选择面试时间', trigger: 'blur' }
        ]
      },
      dialogFormVisible: false,
      pickerOptions: {
        shortcuts: [
          {
            text: '未来三天',
            onClick(picker) {
              const end = new Date()
              const start = new Date()
              end.setTime(start.getTime() + 3600 * 1000 * 24 * 3)
              picker.$emit('pick', [start, end])
            }
          }, {
            text: '未来一周',
            onClick(picker) {
              const end = new Date()
              const start = new Date()
              end.setTime(start.getTime() + 3600 * 1000 * 24 * 7)
              picker.$emit('pick', [start, end])
            }
          },
          {
            text: '未来一个月',
            onClick(picker) {
              const end = new Date()
              const start = new Date()
              end.setTime(start.getTime() + 3600 * 1000 * 24 * 30)
              picker.$emit('pick', [start, end])
            }
          }]
      }
    }
  },

  mounted() {
    this.getRecruitmentOptions()
    this.getList()
  },
  methods: {
    // 获取该求职者发布的职位列表
    getRecruitmentOptions() {
      getRecruitmentOptions().then(
        (response) => {
          this.recruitmentOptions = response.data
        },
        (error) => {
          this.$message.error(error.message)
        }
      )
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    // 获取列表
    getList() {
      if (this.interviewTime != null) {
        this.listQuery.interviewTimeFrom = this.interviewTime[0]
        this.listQuery.interviewTimeTo = this.interviewTime[1]
      }
      // console.log(this.listQuery)
      fetchInterviewList(this.listQuery).then(
        (response) => {
          this.list = response.data.interviewList
          this.total = response.data.count
          // console.log(this.list)
        },
        (error) => {
          this.$message.error(error.message)
        }
      )
    },
    // 重置查询条件
    reset() {
      this.interviewTime = undefined
      this.listQuery = {
        page: 1,
        limit: 10,
        // status: this.query.status,
        recruitmentId: undefined,
        interviewTimeFrom: undefined,
        interviewTimeTo: undefined
      }
      this.getList()
    },
    // 再次发送邀约
    InviteAgain(id) {
      const param = { id: id }
      inviteAgain(param).then(
        (response) => {
          this.$message.success('已成功发送邀约')
          this.getList()
        },
        (error) => {
          this.$message.error(error.message)
        }
      )
    },
    // 撤回邀约
    withdraw(id) {
      const param = { id: id }
      withdrawInterview(param).then(
        (response) => {
          this.$message.success('面试邀约已撤回')
          this.getList()
        },
        (error) => {
          this.$message.error(error.message)
        }
      )
    },
    // 取消邀约
    cancelInterview() {
      this.dialogFormVisible = false
      this.$nextTick(() => {
        this.$refs['ruleForm'].clearValidate()
      })
    },
    // 修改邀约
    handleEdit(id) {
      this.dialogFormVisible = true
      // 加载邀约的数据
      const param = { id: id }
      getOne(param).then(
        (response) => {
          // console.log(response.data)
          this.interviewForm = response.data
        },
        (error) => {
          this.$message.error(error.message)
        }
      )
    },
    // 邀约面试
    submitForm() {
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          updateInterview(this.interviewForm).then(
            (response) => {
              this.dialogFormVisible = false
              this.$message.success('面试邀约修改成功')
              this.getList()
            },
            (error) => {
              this.$message.error(error.message)
            }
          )
        } else {
          return false
        }
      })
    }
  }
}

</script>
<style lang="scss" scoped>
  .app-container {
    min-height: 500px;
  }

  .table-item {
    margin-top: 20px;
  }

  .mr10 {
    margin-right: 10px;
  }

  .ml10 {
    margin-left: 10px;
  }

  .filter-container {
    /*padding-top: 10px;*/
    padding-bottom: 10px;
  }
</style>
